package com.bwda.dsrs.systemservice.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * Request对象处理
 * 
 * @version 1.0 (2008-08-21)
 * @author yanyuemei
 */
public final class RequestUtil {

	/**
	 * 获得指定参数名称的对象
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * @return Object
	 */
	public final static Object getAttributeAsObject(HttpServletRequest request,
			String strParameter) {
		Object objectResult = null;
		if (checkRequest(request)) {
			objectResult = request.getAttribute(strParameter);
		}
		return objectResult;
	}

	/**
	 * 获得指定参数名的Attribute字符串值
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * 
	 * @return String
	 */
	public final static String getAttributeAsString(HttpServletRequest request,
			String strParameter) {
		String strResult = "";
		if (checkRequest(request)) {
			if (strParameter != null && strParameter.trim().length() > 0) {
				Object object = request.getAttribute(strParameter);
				if (object != null) {
					strResult = object.toString();
						strResult = strResult.trim();
				}
				object = null;
			}
		}
		// 释放对象
		request = null;
		strParameter = null;
		return strResult;
	}

	/**
	 * 获得指定参数的Attribute字符串值
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * 
	 * @param strDefaultValue
	 *            缺省值
	 * 
	 * @return String
	 */
	public final static String getAttributeAsString(HttpServletRequest request,
			String strParameter, String strDefaultValue) {
		String strResult = "";
		strResult = getAttributeAsString(request, strParameter);
		if (strResult.length() == 0) {
			if (strDefaultValue != null && strDefaultValue.trim().length() > 0) {
				strResult = strDefaultValue;
			}
		}
		// 释放对象
		request = null;
		strParameter = null;
		strDefaultValue = null;
		return strResult;
	}

	/**
	 * 获得指定参数的字符串值
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * 
	 * @return String
	 */
	public final static String getString(HttpServletRequest request,
			String strParameter) {
		String strResult = "";
		if (checkRequest(request)) {
			if (strParameter != null && strParameter.trim().length() > 0) {
				strResult = request.getParameter(strParameter);
				if (strResult == null) {
					strResult = "";
				} else {
					strResult = strResult.trim();
				}
			}
		}
		// 释放对象
		request = null;
		strParameter = null;
		return strResult;
	}

	/**
	 * 获得同名参数的数组值
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * 
	 * @return String[]
	 */
	public final static String[] getStringAsArray(HttpServletRequest request,
			String strParameter) {
		String[] strArrayResult = null;
		if (checkRequest(request)) {
			if (strParameter != null && strParameter.trim().length() > 0) {
				strArrayResult = request.getParameterValues(strParameter);
			}
		}
		// 释放对象
		request = null;
		strParameter = null;
		return strArrayResult;
	}

	/**
	 * 获得指定参数的字符串值
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * 
	 * @param strDefaultValue
	 *            缺省值
	 * 
	 * @return String
	 */
	public final static String getString(HttpServletRequest request,
			String strParameter, String strDefaultValue) {
		String strResult = "";
		strResult = getString(request, strParameter);
		if (strResult.length() == 0) {
			if (strDefaultValue != null && strDefaultValue.trim().length() > 0) {
				strResult = strDefaultValue;
			}
		}
		// 释放对象
		request = null;
		strParameter = null;
		strDefaultValue = null;
		return strResult;
	}

	/**
	 * 检查Request对象
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return boolean
	 */
	private final static boolean checkRequest(HttpServletRequest request) {
		boolean blnResult = false;
		if (request != null) {
			blnResult = true;
		}
		// 释放对象
		request = null;
		return blnResult;
	}

	/**
	 * 获得指定参数的整数值
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param strParameter
	 *            参数名
	 * 
	 * @param intDefaultValue
	 *            缺省值
	 * 
	 * @return int
	 */
	public final static int getInt(HttpServletRequest request,
			String strParameter, int intDefaultValue) {
		int intResult = 0;
		String strValue = getString(request, strParameter);
		if (strValue.length() > 0) {
			boolean blnDigit = isNumeric(strValue);
			if (blnDigit) {
				// 数字
				intResult = Integer.parseInt(strValue);
			} else {
				// 非数字

				intResult = intDefaultValue;
			}
		} else {
			intResult = intDefaultValue;
		}
		// 释放对象
		request = null;
		strParameter = null;
		strValue = null;
		return intResult;
	}

	/**
	 * 检查是否为数字
	 * 
	 * @param strValue
	 *            被检查值
	 * 
	 * @return boolean
	 */
	public final static boolean isNumeric(String strValue) {
		boolean blnResult = true;
		for (int i = strValue.length() - 1; i >= 0; i--) {
			if (!Character.isDigit(strValue.charAt(i))) {
				blnResult = false;
				break;
			}
		}
		// 释放对象
		strValue = null;
		return blnResult;
	}
}
